package handler

import (
	"github.com/gin-gonic/gin"
	"onerk4/yk/api/client"
	"onerk4/yk/api/enit/request"
	"onerk4/yk/api/enit/response"
	"onerk4/yk/common/proto/user"
)

func UserLogin(c *gin.Context) {
	var req request.UserLogin

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.UserLogin(c, &user.UserLoginRequest{
		Username: req.Username,
		Password: req.Password,
	})
	if err != nil {
		response.ErrorResponse(c, "err", err.Error())
		return
	}

	response.SuccessResponse(c, "ok", login)
}

func AddCourse(c *gin.Context) {
	var req request.AddCourse

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.AddCourse(c, &user.AddCourseRequest{
		CourseName:    req.CourseName,
		CourseInfo:    req.CourseInfo,
		CourseContent: req.CourseContent,
		CoursePrice:   req.CoursePrice,
		Status:        req.Status,
	})
	if err != nil {
		response.ErrorResponse(c, "err", err.Error())
		return
	}

	response.SuccessResponse(c, "ok", login)
}

func CourseList(c *gin.Context) {
	login, err := client.CourseList(c, &user.CourseListRequest{})
	if err != nil {
		response.ErrorResponse(c, "err", err.Error())
		return
	}

	response.SuccessResponse(c, "ok", login)
}

func CourseInfo(c *gin.Context) {
	var req request.CourseInfo

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.CourseInfo(c, &user.CourseInfoRequest{
		Id: req.Id,
	})
	if err != nil {
		response.ErrorResponse(c, "err", err.Error())
		return
	}

	response.SuccessResponse(c, "ok", login)
}

func UpdateCourse(c *gin.Context) {
	var req request.UpdateCourse

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.UpdateCourse(c, &user.UpdateCourseRequest{
		Id:            req.Id,
		CourseName:    req.CourseName,
		CourseInfo:    req.CourseInfo,
		CourseContent: req.CourseContent,
		CoursePrice:   req.CoursePrice,
		Status:        req.Status,
	})
	if err != nil {
		response.ErrorResponse(c, "err", err.Error())
		return
	}

	response.SuccessResponse(c, "ok", login)
}

func AddCoursePanic(c *gin.Context) {
	var req request.AddCoursePanic

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.AddCoursePanic(c, &user.AddCoursePanicRequest{
		CourseId:  req.CourseId,
		Num:       req.Num,
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		Status:    req.Status,
	})
	if err != nil {
		response.ErrorResponse(c, "err", err.Error())
		return
	}

	response.SuccessResponse(c, "ok", login)
}

func AddOrder(c *gin.Context) {
	var req request.AddOrder

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.AddOrder(c, &user.AddOrderRequest{
		CourseId: req.CourseId,
		Num:      req.Num,
	})
	if err != nil {
		response.ErrorResponse(c, "err", err.Error())
		return
	}

	response.SuccessResponse(c, "ok", login)
}
